
do "...\First.do"

* Takes all children (full population) and find their highest level of 
* completed education

********************************************************************************
clear 
use "$raw\Children1.dta"
g yob=year-alder
tab yob

drop alder year
drop kom

format koen %1.0f

duplicates drop
cap drop N
bys pnr: g N=_N 
tab N

drop if far_id=="" & N>1
cap drop N
bys pnr: g N=_N
tab N

drop if mor_id=="" & N>1
cap drop N
bys pnr: g N=_N
tab N 
cap drop N

sort pnr mor_id far_id
bys pnr: g n=_n
keep if n==1
drop n

save "$work\children1.dta", replace
* The dataset contains one observations per person including mother and father id.

********************************************************************************
********************************************************************************

* Merging with educational outcomes
clear 
use "$raw\Children3.dta"

keep pnr hfaudd year

format hfaudd %4.0f
drop if missing(hfaudd)
tostring hfaudd, replace

merge m:1 hfaudd using "I:\Workdata\707068\Ida\formats\hfaudd_level.dta"
drop if _merge==2
drop _merge

sort pnr year

merge m:1 pnr using "$work\children1.dta"
keep if _merge==3
drop _merge

g age=year-yob
drop if age<30 
sort pnr year

cap drop n
bys pnr: g n=_n
tab n

** Keep education at age 35 or at the minimum age if they are not in the data at age 35
bys pnr: egen min_age=min(age)
bys pnr: egen max_age=max(age)

sum min_age
sum max_age

g keep=1 if age==35
bys pnr: egen max_keep=max(keep)

sum min_age if missing(max_keep)
sum max_age if missing(max_keep)
replace keep=1 if age==min_age & max_keep!=1

keep if keep==1
drop keep

order pnr mor_id far_id
drop year age min_age max_age n max_keep

tab yob
duplicates drop


save "$work\children3.dta", replace




